package com.feifan.db;

import javax.servlet.RequestDispatcher;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
 * 用于对数据库进行建立连接
 */
public class DataBase {
    private static Connection connection=null;
    public static Connection getConnection(){
        try {
            Class.forName("com.mysql.jdbc.Driver");//先对Driver类进行反射
            //然后通过数据库的一些要求对其建立连接
            connection= DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/bz?&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false","root","root");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }

    /**
     * 用于关闭数据库
     */
    public static void closeConnection(){
        if(connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
